package atguigu.dao.impl;

import atguigu.dao.AccountDao;
import atguigu.pojo.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;
@Repository
public class AccountDaoImpl implements AccountDao {
    /**
     *  dao层执行SQL语句
     *  依赖QueryRunner对象
     *  注入对象
     */
    @Autowired
    private QueryRunner qr;

    /**
     * 保存账户
     */
    @Override
    public int insertAccount(Account account) throws SQLException {
        String sql = "insert into account values(?,?,?)";
        //account对象的参数，存储在数组中
        Object[] param = {null,account.getName(),account.getMoney()};
        return qr.update(sql,param);
    }

    @Override
    public List<Account> queryAccount() throws SQLException {
        String sql = "select * from account";
        return qr.query(sql,new BeanListHandler<Account>(Account.class));
    }
}
